package com.yiwei.hdb.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.hdb.R;
import com.yiwei.hdb.adapter.base.ItemAdapter;
import com.yiwei.hdb.model.BankCardListModel;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class BankCardListAdapter extends ItemAdapter<BankCardListModel.DataBean, BankCardListAdapter.DefaultHolder> {
    private int[] bankImg = {R.mipmap.bank_default, R.mipmap.guangfa, R.mipmap.huaxia, R.mipmap.shanghai, R.mipmap.xingye, R.mipmap.zhaoshang, R.mipmap.gongshang, R.mipmap.nongye, R.mipmap.zhongguoyinhang, R.mipmap.zhongxin, R.mipmap.minsheng, R.mipmap.jianshe, R.mipmap.guangda, R.mipmap.pufa, R.mipmap.youzheng, R.mipmap.nongcun, R.mipmap.jiaotong};

    private Context context;
    private int[] bg_res = new int[]{R.mipmap.img_bankcar_bg_one, R.mipmap.img_bankcar_bg_two, R.mipmap.img_bankcar_bg_three, R.mipmap.img_bankcar_bg_four};

    public BankCardListAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_bank_list_v3, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        BankCardListModel.DataBean item = getItem(position);
        holder.layoutTotal.setBackgroundResource(bg_res[position % 4]);
        holder.txtName.setText(item.bank.bank_card_type);
        holder.txtType.setText(item.card_type);
        if (!TextUtils.isEmpty(item.bankcard) && item.bankcard.length() >= 5) {
            holder.txtCardNo.setText("**** **** **** " + item.bankcard.substring(item.bankcard.length() - 4, item.bankcard.length()));
        }

        getBankImg(item.bank.bank_card_type, holder.imgIcon);
//        if (item.bank.name.contains("工商")) {
//            Glide.with(context).load(R.mipmap.img_bank_gongshang).into(holder.imgIcon);
//        } else if (item.bank.name.contains("广发")) {
//            Glide.with(context).load(R.mipmap.img_bank_guangfa).into(holder.imgIcon);
//        } else if (item.bank.name.contains("光大")) {
//            Glide.with(context).load(R.mipmap.img_bank_guangda).into(holder.imgIcon);
//        } else if (item.bank.name.contains("广州")) {
//            Glide.with(context).load(R.mipmap.img_bank_guangzhou).into(holder.imgIcon);
//        } else if (item.bank.name.contains("建设")) {
//            Glide.with(context).load(R.mipmap.img_bank_jianshe).into(holder.imgIcon);
//        } else if (item.bank.name.contains("交通")) {
//            Glide.with(context).load(R.mipmap.img_bank_jiaotong).into(holder.imgIcon);
//        } else if (item.bank.name.contains("平安")) {
//            Glide.with(context).load(R.mipmap.img_bank_pingan).into(holder.imgIcon);
//        } else if (item.bank.name.contains("浦发")) {
//            Glide.with(context).load(R.mipmap.img_bank_pufa).into(holder.imgIcon);
//        } else if (item.bank.name.contains("兴业")) {
//            Glide.with(context).load(R.mipmap.img_bank_xingye).into(holder.imgIcon);
//        } else if (item.bank.name.contains("邮政")) {
//            Glide.with(context).load(R.mipmap.img_bank_youzheng).into(holder.imgIcon);
//        } else if (item.bank.name.contains("浙商")) {
//            Glide.with(context).load(R.mipmap.img_bank_zheshang).into(holder.imgIcon);
//        } else if (item.bank.name.contains("中国")) {
//            Glide.with(context).load(R.mipmap.img_bank_zhongguo).into(holder.imgIcon);
//        } else if (item.bank.name.contains("中信")) {
//            Glide.with(context).load(R.mipmap.img_bank_zhongxin).into(holder.imgIcon);
//        } else {
//            Glide.with(context).load(R.drawable.bg_circle_blue).into(holder.imgIcon);
//        }
        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (shareClickListener != null) {
                    shareClickListener.onClick(item);
                }
            }
        });
    }

    public interface OnShareClickListener {
        void onClick(BankCardListModel.DataBean item);
    }

    private OnShareClickListener shareClickListener;

    public void setShareClickListener(OnShareClickListener shareClickListener) {
        this.shareClickListener = shareClickListener;
    }

    private void getBankImg(String bankName, ImageView icon) {
        try {
            icon.setImageDrawable(ContextCompat.getDrawable(context, bankImg[0]));
            if (TextUtils.isEmpty(bankName)) return;
            int id = 0;
            if (bankName.contains(context.getString(R.string.guangfa))) id = 1;
            if (bankName.contains(context.getString(R.string.huaxia))) id = 2;
            if (bankName.contains(context.getString(R.string.shanghai))) id = 3;
            if (bankName.contains(context.getString(R.string.xingye))) id = 4;
            if (bankName.contains(context.getString(R.string.zhaoshang))) id = 5;
            if (bankName.contains(context.getString(R.string.gongshang))) id = 6;
            if (bankName.contains(context.getString(R.string.nongye))) id = 7;
            if (bankName.contains(context.getString(R.string.zhongguoyinhang)))
                id = 8;
            if (bankName.contains(context.getString(R.string.zhongxin))) id = 9;
            if (bankName.contains(context.getString(R.string.minsheng))) id = 10;
            if (bankName.contains(context.getString(R.string.jianshe))) id = 11;
            if (bankName.contains(context.getString(R.string.guangda))) id = 12;
            if (bankName.contains(context.getString(R.string.pufa))) id = 13;
            if (bankName.contains(context.getString(R.string.youzheng))) id = 14;
            if (bankName.contains(context.getString(R.string.nongcun))) id = 15;
            if (bankName.contains(context.getString(R.string.jiaotong))) id = 16;

            icon.setImageDrawable(ContextCompat.getDrawable(context, bankImg[id]));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_icon)
        ImageView imgIcon;
        @BindView(R.id.txt_name)
        TextView txtName;
        @BindView(R.id.txt_type)
        TextView txtType;
        @BindView(R.id.txt_card_no)
        TextView txtCardNo;

        @BindView(R.id.layout_total)
        RelativeLayout layoutTotal;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
